package com.fenghuajueli.module_user.activity.register;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.blankj.utilcode.util.ToastUtils;
import com.fenghuajueli.module_route.UserModuleRoute;
import com.fenghuajueli.module_user.R;
import com.fenghuajueli.module_user.activity.UserViewModelBaseActivity;
import com.fenghuajueli.module_user.constants.TypeConstant;
import com.fenghuajueli.module_user.databinding.ActivityPasswordSetBinding;
import com.fenghuajueli.libbasecoreui.viewmodel.BaseViewModel;
import com.fenghuajueli.module_user.model.UserViewModel;

/**
 * 密码设置界面,目前默认是忘记密码功能的设置密码界面
 */
@Route(path = UserModuleRoute.USER_PASSWORD_SET_ACTIVITY)
public class PasswordSetActivity extends UserViewModelBaseActivity implements View.OnClickListener {

    @Autowired(name = "phone")
    String phone;
    @Autowired(name = "type")
    String type;
    @Autowired(name = "code")
    String code;
    private boolean passwordIsInputComplete = false;
    private boolean passwordIsConfirmInputComplete = false;
    private boolean pwdIsShow = false;
    private boolean pwdConfirmIsShow = false;

    ActivityPasswordSetBinding binding;
    private final UserViewModel userViewModel = new UserViewModel();

    @Override
    public BaseViewModel setBaseViewModel() {
        return userViewModel;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityPasswordSetBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        initView();
    }

    private void initView() {
        binding.etInputPassword.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (TextUtils.isEmpty(s.toString().trim())) {
                    passwordIsInputComplete = false;
                } else {
                    if (s.toString().trim().length() < 8) {
                        passwordIsInputComplete = false;
                    } else {
                        passwordIsInputComplete = true;
                    }
                }
                changeButtonStatus();
            }
        });
        binding.etInputConfirmPassword.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (TextUtils.isEmpty(s.toString().trim())) {
                    passwordIsConfirmInputComplete = false;
                } else {
                    if (s.toString().trim().length() < 8) {
                        passwordIsConfirmInputComplete = false;
                    } else {
                        passwordIsConfirmInputComplete = true;
                    }

                }
                changeButtonStatus();
            }
        });
        if (TypeConstant.VerCodeType.REGISTER.name().equals(type)) {
            if (TextUtils.isEmpty(phone)) {
                ToastUtils.showShort("手机号不能为空！");
                finish();
                return;
            }
            binding.btnGoNext.setText("完成注册");
            binding.tvCodeType.setText("填写密码");
            binding.loginTypeTips.setText("请输入密码并点击完成注册来创建新账号。");
        } else if (TypeConstant.VerCodeType.FORGIVE.name().equals(type)) {
            if (TextUtils.isEmpty(phone)) {
                ToastUtils.showShort("手机号不能为空！");
                finish();
                return;
            }
            if (TextUtils.isEmpty(code)) {
                ToastUtils.showShort("验证码不能为空！");
                finish();
                return;
            }
            binding.btnGoNext.setText("重置密码");
            binding.tvCodeType.setText("密码重置");
            binding.loginTypeTips.setText("请重复填写2次密码来重置你的登录密码，需要牢记。");
        } else {
            ToastUtils.showShort("没有对应类型！");
            finish();
            return;
        }
        binding.btnGoNext.setOnClickListener(this);
        binding.ivClose.setOnClickListener(this);
        binding.ivConfirmPwdStatus.setOnClickListener(this);
        binding.ivChangePwdStatus.setOnClickListener(this);
    }

    private void changeButtonStatus() {
        if (passwordIsInputComplete && passwordIsConfirmInputComplete) {
            binding.btnGoNext.setEnabled(true);
        } else {
            binding.btnGoNext.setEnabled(false);
        }
    }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.btnGoNext) {
            if (TypeConstant.VerCodeType.FORGIVE.name().equals(type)) {
                //进行修改密码
                changePwd();
            }
        } else if (view.getId() == R.id.ivClose) {
            finish();
        } else if (view.getId() == R.id.ivChangePwdStatus) {
            if (pwdIsShow) {
                binding.etInputPassword.setTransformationMethod(PasswordTransformationMethod.getInstance());
                binding.ivChangePwdStatus.setImageResource(R.mipmap.aa_icon_to_view);
            } else {
                binding.etInputPassword.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                binding.ivChangePwdStatus.setImageResource(R.mipmap.aa_icon_close_look_at);
            }
            pwdIsShow = !pwdIsShow;
            binding.etInputPassword.postInvalidate();
        } else if (view.getId() == R.id.ivConfirmPwdStatus) {
            if (pwdConfirmIsShow) {
                binding.etInputConfirmPassword.setTransformationMethod(PasswordTransformationMethod.getInstance());
                binding.ivConfirmPwdStatus.setImageResource(R.mipmap.aa_icon_to_view);
            } else {
                binding.etInputConfirmPassword.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                binding.ivConfirmPwdStatus.setImageResource(R.mipmap.aa_icon_close_look_at);
            }
            pwdConfirmIsShow = !pwdConfirmIsShow;
            binding.etInputConfirmPassword.postInvalidate();
        }
    }


    private void changePwd() {
        if (userViewModel != null) {
            userViewModel.editPassWord(this, phone, binding.etInputPassword.getText().toString().trim(),
                    binding.etInputConfirmPassword.getText().toString().trim(), code);
        }

    }
}